﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Windows.Forms;

using Styx.Common;
using Styx;

namespace OCBotBase
{
    class oc_DataStore
    {
        string CollectionFileLoc;

        List<Mount> MountList = new List<Mount>();
        List<Pet> PetList = new List<Pet>();
        List<Toy> ToyList = new List<Toy>();
        List<Vendor> VendorList = new List<Vendor>();

        private string botDir = Application.StartupPath + "\\Bots\\oc-botbase\\";

        public oc_DataStore(string filePath)
        {
            CollectionFileLoc = filePath;
            loadData();

        }

        public void loadData()
        {
            if (CollectionFileLoc != null)
            {
                XDocument xmlDoc = XDocument.Load(botDir + CollectionFileLoc);
                
                //Build Mount List
                IEnumerable<XElement> element = xmlDoc.Descendants("MountList").Elements();
                //Logging.Write("[OCBot] Nodes:" + element.Count());

                foreach (XElement e in element)
                {
                    Logging.Write("[OCBot] Node Name:" + e.Name);
                    Mount m = new Mount();
                    foreach (XElement p in e.Elements())
                    {
                        //Logging.Write("[OCBot] Name : {0} Value : {1}", p.Name, p.Value);
                        m.setValue(p.Name.ToString(), p.Value.ToString());
                    }
                    MountList.Add(m);
                }
                Logging.Write("[OCBot] Mounts: {0}", MountList.Count);

                //Build Pet List

                element = xmlDoc.Descendants("Pets").Elements();
                //Logging.Write("[OCBot] Nodes:" + element.Count());

                foreach (XElement e in element)
                {
                    Logging.Write("[OCBot] Node Name:" + e.Name);
                    Pet m = new Pet();
                    foreach (XElement p in e.Elements())
                    {
                        //Logging.Write("[OCBot] Name : {0} Value : {1}", p.Name, p.Value);
                        m.setValue(p.Name.ToString(), p.Value.ToString());
                    }
                    PetList.Add(m);
                }
                Logging.Write("[OCBot] Pets: {0}", PetList.Count);

                //Build Toy List

                element = xmlDoc.Descendants("Toys").Elements();
                //Logging.Write("[OCBot] Nodes:" + element.Count());

                foreach (XElement e in element)
                {
                    Logging.Write("[OCBot] Node Name:" + e.Name);
                    Toy m = new Toy();
                    foreach (XElement p in e.Elements())
                    {
                        //Logging.Write("[OCBot] Name : {0} Value : {1}", p.Name, p.Value);
                        m.setValue(p.Name.ToString(), p.Value.ToString());
                    }
                    ToyList.Add(m);
                }
                Logging.Write("[OCBot] Pets: {0}", ToyList.Count);

                //VendorList

                element = xmlDoc.Descendants("Vendors").Elements();
                //Logging.Write("[OCBot] Nodes:" + element.Count());

                foreach (XElement e in element)
                {
                    Logging.Write("[OCBot] Node Name:" + e.Name);
                    Vendor m = new Vendor();
                    foreach (XElement p in e.Elements())
                    {
                        //Logging.Write("[OCBot] Name : {0} Value : {1}", p.Name, p.Value);
                        m.setValue(p.Name.ToString(), p.Value.ToString());
                    }
                    VendorList.Add(m);
                }
                Logging.Write("[OCBot] Pets: {0}", VendorList.Count);


                // Get current character mounts

                List<Styx.CommonBot.Mount.MountWrapper> currMounts = Styx.CommonBot.Mount.Mounts;
                Logging.Write("[OCBot] Known Mounts: {0}, or mabey {1}", currMounts.Count, Styx.CommonBot.Mount.NumMounts);
                
                //List known Mounts
                //foreach (Styx.CommonBot.Mount.MountWrapper m in currMounts)
                //{
                //    Logging.Write(m.Name);
                //}

                //Check which mounts we need && know how to get

                List<Mount> TempList = new List<Mount>();

                foreach (Mount m in MountList)
                {
                    //Logging.Write("Checking if {0} exists in know mount list", m.Name);
                    if (!currMounts.Where(s => s != null).Any(s => s.CreatureSpellId == m.SpellID))
                    {
                        Logging.Write("[OCBot] Found Mount {0}, Need it", m.Name, m.SpellID);
                        TempList.Add(m);
                        //Logging.Write("Added Mount {0} with ID {1}.", m.Name, m.SpellID);
                    }
                }

                MountList = TempList;
                Logging.Write("[OCBot] Mounts to Get: {0}", MountList.Count);
                

            }
        }
    }
}
